#include "combat.h"
#include "enemy.h"
#include "player.h"

combat::combat()
{
	totalDmg = 0;
	playerName = "Berk";
	playerMaxHealth = 150;
	minDmg = 5;
	maxDmg = 10;
	enemyName = "Grue";
	enemyMaxHealth = 100;
	foeMinDmg = 3;
	foeMaxDmg = 8;
	critChance = 5;
	dodgeChance = 5;
	parryChance = 20;
	counterChance = 10;
	missChance = 5;
}

combat::combat(player &currPlayer, enemy &currEnemy)
{
	totalDmg = 0;
	playerName = currPlayer.getName();
	currPlayer.healthCalc();
	playerMaxHealth = currPlayer.getMaxHealth();
	minDmg = currPlayer.getMinDmg();
	maxDmg = currPlayer.getMaxDmg();
	enemyName = currEnemy.getName();
	currEnemy.healthCalc();
	enemyMaxHealth = currEnemy.getMaxHealth();
	foeMinDmg = currEnemy.getMinDmg();
	foeMaxDmg = currEnemy.getMaxDmg();
	critChance = currPlayer.getCrit();
	dodgeChance = currPlayer.getDodge();
	parryChance = 20;		//parryChance = currPlayer.getParry();
	counterChance = 10;		//counterChance = currPlayer.getCounter();
	missChance = 5;			//missChance = currPlayer.getMiss();
}

combat::~combat()
{
}

void combat::initCombatVars(player currPlayer, enemy currEnemy)
{
	totalDmg = 0;
	playerName = currPlayer.getName();
	currPlayer.healthCalc();
	playerMaxHealth = currPlayer.getMaxHealth();
	minDmg = currPlayer.getMinDmg();
	maxDmg = currPlayer.getMaxDmg();
	enemyName = currEnemy.getName();
	currEnemy.healthCalc();
	enemyMaxHealth = currEnemy.getMaxHealth();
	foeMinDmg = currEnemy.getMinDmg();
	foeMaxDmg = currEnemy.getMaxDmg();
	critChance = currPlayer.getCrit();
	dodgeChance = currPlayer.getDodge();
	parryChance = 20;		//parryChance = currPlayer.getParry();
	counterChance = 10;		//counterChance = currPlayer.getCounter();
	missChance = 5;			//missChance = currPlayer.getMiss();
}

int combat::randNum(int min, int max)
{
	int randNumber = min + rand() % (max - min + 1);

	return randNumber;
}

void combat::printHealth(double health, double maxHealth)
{
	double healthPerc = (health / maxHealth) * 100;

	if(healthPerc == 100)
		cout << "Uninjured";
	else if(healthPerc >= 90)
		cout << "Barely Injured";
	else if(healthPerc >= 75)
		cout << "Injured";
	else if(healthPerc >= 50)
		cout << "Wounded";
	else if(healthPerc >= 25)
		cout << "Badly Wounded";
	else if(healthPerc >= 1)
		cout << "Near Death";
	else if(healthPerc < 1)
		cout << "Dead";

	cout << endl;
}

int combat::attack(int minDmg, int maxDmg, string name, double &health, int fminDmg, int fmaxDmg, string foeName, double &foeHealth)
{
	bool dodge = false;
	bool parry = false;
	int dmg = 0;

	if(randNum(1, 100) > missChance)
	{
		if(randNum(1, 100) <= dodgeChance)
		{
			//dodge = true;
			cout << foeName << " DODGED " << name << "'s attack.\n";
		}
		else if(randNum(1, 100) <= parryChance)
		{
			//parry = true;
			cout << foeName << " PARRIES " << name << "'s attack!\n";
			if(randNum(1, 100) <= counterChance)
				counter(fminDmg, fmaxDmg, foeName, foeHealth, minDmg, maxDmg, name, health);
		}
		else
		{
			dmg = randNum(minDmg, maxDmg);	
			cout << name << " hit " << foeName << " for " << dmg << " damage!\n";
			foeHealth -= dmg;
		}
	}
	else
		cout << name << " MISSES!\n";
	
		
	return dmg;
}

int combat::counter(int minDmg, int maxDmg, string name, double health, int fminDmg, int fmaxDmg, string foeName, double &foeHealth)
{
	bool dodge = false;
	bool parry = false;
	int dmg = 0;

	if(randNum(1, 100) <= dodgeChance)
	{
		dodge = true;
		cout << foeName << " DODGED your attack.\n";
	}
	else if(randNum(1, 100) <= parryChance)
	{
		parry = true;
		cout << foeName << " PARRIES " << name << "'s counter!\n";
		if(randNum(1, 100) <= counterChance)
			counter(fminDmg, fmaxDmg, foeName, foeHealth, minDmg, maxDmg, name, health);
	}
	else
	{
		dmg = randNum(minDmg, maxDmg);
		cout << name << " counters for " << dmg << " damage!\n";
		foeHealth -= dmg;
	}
		
	return dmg;
}

void combat::battle()
{
	char combatOption;
	bool continueCombat = true;
	playerHealth = playerMaxHealth;
	enemyHealth = enemyMaxHealth;
	
	cout << "You are under attack! 'x' quits." << endl;

	while(continueCombat)
	{		
		//Main combat menu
		cout << endl;
		cout << playerName << "'s Health: ";
		printHealth(playerHealth, playerMaxHealth);
		cout << enemyName << "'s Health: ";
		printHealth(enemyHealth, enemyMaxHealth);
		cout << "1. Attack\n2. Flee\n>> ";
		cin >> combatOption;

		if(combatOption == 'x')
			continueCombat = false;

		switch(combatOption)
		{
			case '1':
				cout << endl;
				
				//player attacks
				attack(minDmg, maxDmg, playerName, playerHealth, foeMinDmg, foeMaxDmg, enemyName, enemyHealth);
				//enemy attacks
				attack(foeMinDmg, foeMaxDmg, enemyName, enemyHealth, minDmg, maxDmg, playerName, playerHealth);

				//check if anyone is dead
				if(enemyHealth <= 0 || playerHealth <= 0)
				{
					cout << "Player Health: ";
					printHealth(playerHealth, playerMaxHealth);
					cout << "Enemy Health: ";
					printHealth(enemyHealth, enemyMaxHealth);
					continueCombat = false;
				}
				
				
				//set enemy health down
				
				//if parried()
				
				//playerDmgDone += finalDmg;

				//checks to see if your hit kills the creature, so it cannot hit when dead
				//is enemy able to attack back or dead?

				//Enemies attack, enemy values passed into function parameters
				//enemy gets to retaliate

				//do you block their shit?

				//set health		
				break;

			case '2':
				//calls function to make flee either true or false
				//can you flee?

				//If flee is true, no more is done, but flee is still marked as true so the while clause will cut out
				//if you dont successfully flee the enemies get a hit
				break;

			case 'x':
				cout << "Ending simluation.\n";
				break;

			default:
				cout << endl;
				cout << "Invalid option!" << endl;
				break;
		}
		
		/*if(playerChar->getHealth() <= 0)
		{
			playerChar->setHealth(0);
			combatMsg->deathMsg(playerChar->getName());
		}

		cout << endl;
		cout << playerChar->getName() << ": ";
		printHealth((double)playerChar->getHealth(), (double)playerChar->getMaxHealth());
		//cout << playerMaxHealth << endl;
		cout << endl;
	}
	
		//this is the first if called because otherwise if you flee mid combat you get an eaten msg, when you in fact run
		if(isFleeing == true)
			combatMsg->fleeMsg(playerChar->getName());
		else if(playerChar->getHealth() == 0)
		cout << "You were defeated in battle and eaten." << endl;
		/*else if(grueHappy)
			cout << "You and the " << enemyName << " became friends." << endl;*/
			
	}
}